package font;

import java.awt.Color;
import java.awt.FontMetrics;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import javax.swing.JLabel;

import config.ConfigType;


public class Font 
{
	private String m_name;
	private String m_fontName;
	private HashMap<Character, CharInfo> m_charMap;
	private java.awt.Font m_font;
	private Color m_color;
	private int m_height;
	private int m_descent;
	private boolean m_isWhole;

	public Font(String name, String fontName, int size)
	{
		m_name = name;
		m_fontName = fontName;
		int style = 0;
		if(ConfigType.E_FONT_BOLD_CT.getBoolValue())
		{
			style |= java.awt.Font.BOLD;
		}
		if(ConfigType.E_FONT_ITALIC_CT.getBoolValue())
		{
			style |= java.awt.Font.ITALIC;
		}
		if(style == 0)
		{
			style = java.awt.Font.PLAIN;
		}
		m_font = new java.awt.Font(m_fontName, style, size);
		m_charMap = new HashMap<Character, CharInfo>();
		m_color = ConfigType.E_FONT_FG_COLOR_CT.createColor(ConfigType.E_FONT_FG_COLOR_CT.getLongValue());
		FontMetrics fm = new JLabel().getFontMetrics(m_font);
		m_height = fm.getHeight();
		m_descent = fm.getDescent();
		m_isWhole = ConfigType.E_FONT_WHOLE_CT.getBoolValue();
	}
	
	public void addToFontAtlas(FontAtlas fa)
	{
		
	}
	
	public String getName()
	{
		return m_name;
	}
	
	public int getDescent()
	{
		return m_descent;
	}
	
	public Color getColor()
	{
		return m_color;
	}
	
	public java.awt.Font getFont()
	{
		return m_font;
	}
	
	public boolean isWhole()
	{
		return m_isWhole;
	}
	
	public void addChar(StringBuffer str)
	{
		FontMetrics fm = new JLabel().getFontMetrics(m_font);
		for(int i = 0, max = str.length(); i < max; ++i)
		{
			char ch = str.charAt(i);
			if(!m_charMap.containsKey(ch))
			{
				CharInfo ci = new CharInfo(this, ch);
				ci.m_fontW = fm.charWidth(ch);
				m_charMap.put(ch, ci);
			}
		}
	}
	
	public StringBuffer getChars()
	{
		StringBuffer text = new StringBuffer("");
		Iterator<Entry<Character, CharInfo>> it = m_charMap.entrySet().iterator();
		while(it.hasNext())
		{
			text.append(it.next().getKey());
		}
		return text;
	}
	
	public void addToAtlas(FontAtlas atlas)
	{
		int padding = ConfigType.E_FONT_PADDING_CT.getIntValue();
		Iterator<Entry<Character, CharInfo>> it = m_charMap.entrySet().iterator();
		int max = 0;
		while(it.hasNext())
		{
			Entry<Character, CharInfo> entry = it.next();
			CharInfo info = entry.getValue();
			info.m_w = info.m_fontW + padding;
			info.m_h = m_height + padding;
			if(m_isWhole)
			{
				if(max < info.m_w)
				{
					max = info.m_w;
				}
			}
		}
		if(m_isWhole)
		{
			it = m_charMap.entrySet().iterator();
			while(it.hasNext())
			{
				Entry<Character, CharInfo> entry = it.next();
				entry.getValue().m_w = max;
			}
		}
		it = m_charMap.entrySet().iterator();
		while(it.hasNext())
		{
			Entry<Character, CharInfo> entry = it.next();
			CharInfo info = entry.getValue();
			atlas.addBlock(info);
		}
	}
}
